package com.ys.nms.module.business.controller.admin.devices;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import com.ys.nms.framework.excel.core.util.ExcelUtils;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import com.ys.nms.module.business.controller.admin.devices.vo.DevicesPageReqVO;
import com.ys.nms.module.business.controller.admin.devices.vo.DevicesRespVO;
import com.ys.nms.module.business.controller.admin.devices.vo.DevicesSaveReqVO;
import com.ys.nms.module.business.dal.dataobject.devices.DevicesDO;
import com.ys.nms.module.business.service.devices.DevicesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设备")
@RestController
@RequestMapping("/business/devices")
@Validated
public class DevicesController {

    @Resource
    private DevicesService devicesService;

    @PostMapping("/create")
    @Operation(summary = "创建设备")
    public CommonResult<Integer> createDevices(@Valid @RequestBody DevicesSaveReqVO createReqVO) {
        return success(devicesService.createDevices(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备")
    public CommonResult<Boolean> updateDevices(@Valid @RequestBody DevicesSaveReqVO updateReqVO) {
        devicesService.updateDevices(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除设备")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDevices(@RequestParam("id") Integer id) {
        devicesService.deleteDevices(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备")
    public CommonResult<Boolean> deleteDevicesList(@RequestParam("ids") List<Integer> ids) {
        devicesService.deleteDevicesListByIds(ids);
        return success(true);
    }

    @Slave
    @GetMapping("/get")
    @Operation(summary = "获得设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DevicesRespVO> getDevices(@RequestParam("id") Integer id) {
        DevicesDO devices = devicesService.getDevices(id);
        return success(BeanUtils.toBean(devices, DevicesRespVO.class));
    }



    @TenantIgnore
    @Slave
    @GetMapping("/page")
    @Operation(summary = "获得设备分页")
    public CommonResult<PageResult<DevicesRespVO>> getDevicesPage(@Valid DevicesPageReqVO pageReqVO) {
        PageResult<DevicesDO> pageResult = devicesService.getDevicesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DevicesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDevicesExcel(@Valid DevicesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DevicesDO> list = devicesService.getDevicesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备.xls", "数据", DevicesRespVO.class,
                        BeanUtils.toBean(list, DevicesRespVO.class));
    }

}